package com.ziyun.pop.mall.module.register.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.service.common.constant.AppType;
import com.service.common.constant.UserType;
import com.service.common.model.UserInfo;
import com.service.ucenter.dao.UserBaseInfoDao;
import com.service.ucenter.dao.UserLevelConfigDao;
import com.service.ucenter.dao.UserLevelDao;
import com.service.ucenter.dao.UserWalletDao;
import com.service.ucenter.module.login.entity.UserBaseInfo;
import com.service.ucenter.module.login.entity.UserLevel;
import com.service.ucenter.module.login.entity.UserLevelConfig;
import com.service.ucenter.module.login.entity.UserWallet;
import com.utility.DateUtils;
import com.ziyun.pop.mall.module.register.model.RegisterVO;

/**
 * 
 * @author wang.jie
 * @date 2018年10月26日 下午8:04:38
 */

@Service
@Transactional
public class RegisterService {

	// private static final String API = "http://192.168.1.36:8088";

	@Autowired
	UserBaseInfoDao userBaseInfoDao;
	@Autowired
	UserLevelDao userMemberLevelDao;
	@Autowired
	UserWalletDao userWalletDao;
	@Autowired
	UserLevelConfigDao userLevelConfigDao;

	public Long registerUserApi(RegisterVO registerVO) {
		// 先生成买家userId 账号
		UserInfo userInfo = registerMallUser(registerVO);
		if (userInfo == null) {
			return null;
		}

		return userInfo.getUserId();
	}

	public UserInfo registerMallUser(RegisterVO registerVO) {
		String account = registerVO.getSellerName();
		Long sellerId = registerVO.getSellerId();
		UserType userType = UserType.pop;
		UserInfo userVO = initMallUser(userType, account, sellerId);
		return userVO;
	}

	private UserInfo initMallUser(UserType userType, String account, Long sellerId) {
		// 初始化用户基础信息
		UserBaseInfo userBaseInfo = UserBaseInfo.builder().build();
		userBaseInfo.setName(account);
		userBaseInfo.setCurrency("CNY");
		userBaseInfo.setUserSource(AppType.erp);
		userBaseInfo.setUserType(userType);
		userBaseInfo.setRegisterSource("pop");
		userBaseInfo.setSupplierId(sellerId);
		userBaseInfo.setRegtime(DateUtils.getCurrentSecond());

		try {
			UserBaseInfoDao.instance.insertSelective(userBaseInfo);
		} catch (Exception e) {
			return null;

		}

		// 用户id回写
		Long userId = userBaseInfo.getUserId();

		UserInfo userInfo = UserInfo.valueOf(userType, userId, account, null, null);

		// 初始化用户会员等级表
		UserLevel userLevel = UserLevel.builder().userId(userId).userLvId(1).build();
		UserLevelDao.instance.insertSelective(userLevel);

		// 初始化用户会员等级配置表
		UserLevelConfig userLevelConfig = UserLevelConfigDao.instance.selectById(userLevel.getUserLvId());

		// 初始化用户钱包表
		Integer createtime = DateUtils.getCurrentSecond();
		UserWallet userWallet = UserWallet.builder().userId(userId).point(10).passwordAccount(account)
				.createtime(createtime).build();
		userWallet.setPoint(10);
		UserWalletDao.instance.insertSelective(userWallet);
		// 积分记录
		userInfo.setLoginAccount(account);
		userInfo.setSex(userBaseInfo.getSex());
		userInfo.setName(userBaseInfo.getName());
		userInfo.setZip(userBaseInfo.getZip());
		userInfo.setUserLevel(userLevelConfig.getName());
		userInfo.setLevelNameId(userLevelConfig.getId().toString());
		userInfo.setNewRegister(true);

		return userInfo;

	}

}